#!/usr/bin/perl -w

#  FILE:   add-notes.pl
#  AUTHOR: JOSE A. ESQUIVEL
#  DATE:   07/12/2001
#
#  DESCRIPTION: Muestra la barra de herramientas para hacer las antoaciones

#inicializar:
require ("init.pl");
require ("dbData.pl");
require ("dbNotes.pl");
require ("dbEvent.pl");
require ("dbOpe.pl");

#imprimir cabecera
&print_header;


#Se define el metdo utilizado para enviar datos
if ('GET' eq $ENV{'REQUEST_METHOD'})
{
  %input = &parse_input;
  
  $dbName        = $input{b};
  $idDescription = $input{d};
  $idStructure   = $input{s};
  $idTopic       = $input{t};
}
else
{
  use CGI;
  
  $q = new CGI;
 
  $dbName        = $q->param("b");
  $idDescription = $q->param("d");
  $idStructure   = $q->param("s");
  $idTopic       = $q->param("t");
  
  $Login         = $q->param("login");
  $Password      = $q->param("password");
  $Nota          = $q->param("nota");
}

#Se obtienen path de imagenes
($dbLenguaje, $IMG) = &get_language($dbName);

%data = &get_data($dbName, "Topic", "idTopic", $idTopic); 
#Se obtiene el nombre del tema en cuestion
$nameTopic = $data{nameTopic.1};
#Se obtiene el URL del tema en cuestion
$URL = $data{URL.1};
#Se obtiene el path de los frames a mostrar
$path = &get_path($URL);

%textos   = &get_data($dbLenguaje, "MyForm", "name", "\'add-notes.pl\'");


$cancel_accion = $path . "controlNT.pl?b=$dbName&t=$idTopic&s=$idStructure&d=$idDescription";
$accion        = $path . "add-notes.pl";


$msg1 = $textos{'inData3'.1};
$msg2 = $textos{'inData4'.1};
$msg3 = $textos{'inData5'.1};

print <<EOFJ0;
<SCRIPT LANGUAJE="Javascript">
<!--

function ShowMsg(txt)
{
    if (txt != "")
    {
	alert(txt);
    }
    return (false);
}

function SubmitForm( )
{
    if (document.myform.login.value == "")
    {
	return(ShowMsg("$msg1"));
    }
     if (document.myform.password.value == "")
    {
	return(ShowMsg("$msg2"));
    }
     if (document.myform.nota.value == "")
    {
	return(ShowMsg("$msg3"));
    }	
    return(true);
}

// -->
</SCRIPT>
EOFJ0
    ;


print <<EOF1;
      <HTML>
	  <HEAD><BASE TARGET="structure"> </HEAD>
          <BODY  BACKGROUND=\"../$IMG/kc.gif\">
EOF1
    ;

if ('GET' eq $ENV{'REQUEST_METHOD'})
{
print <<EOF2
          <CENTER>
             <H2>$textos{'title'.1}</H2>
          </CENTER><P>
	  <FORM NAME="myform" ACTION="$accion"  METHOD="POST"  onsubmit="return SubmitForm( )">
              <TABLE>
  
              <INPUT TYPE="HIDDEN" NAME="b" VALUE="$dbName">
	      <INPUT TYPE="HIDDEN" NAME="d" VALUE="$idDescription">
	      <INPUT TYPE="HIDDEN" NAME="s" VALUE="$idStructure">
              <INPUT TYPE="HIDDEN" NAME="t" VALUE="$idTopic">
              
	      <TR><TD>$textos{'subtitle'.1}<TR>
		  <TD><INPUT TYPE="TEXT"     NAME=\"login\"    SIZE=30  MAXLENGTH=100></TD>
              <TR><TD>$textos{'inData'.1}<TR>
		  <TD><INPUT TYPE="PASSWORD" NAME=\"password\" SIZE=30  MAXLENGTH=100></TD>
              <TR><TD>$textos{'inData2'.1}<TR>
		  <TD><TEXTAREA              NAME=\"nota\" ROWS=15 COLS=27></TEXTAREA></TD>

              </TABLE>
              <TABLE>
                  <TD>                       <INPUT TYPE="IMAGE" SRC="../$IMG/bSend.gif" BORDER=0>
                  <TD><A HREF=$cancel_accion><IMG SRC="../$IMG/bCancel.gif" BORDER=0></A>
              </TABLE>
          </FORM>
EOF2
    ;
}
else
{
    #Validando datos de autor
    $CORRECT = 0; $EXIST = 0;
    %dAuthor = &get_data($dbName, "Author", "nameAuthor", "\'$Login\'");

    if ($dAuthor{numRows} > 0)
    {
	#Existe el author
	$EXIST = 1;
	$idAuthor = $dAuthor{'idAuthor'.1};
	if ($dAuthor{'password'.1} eq $Password)
	{
	    $CORRECT = 1;
	}
    }
    if (($EXIST==1) && ($CORRECT == 1))
    {
	#Verificando permiso
	$uAuthor = $dAuthor{'typeUser'.1};

	%Usuario = &get_data($dbName, "User", "idUser", "$uAuthor");

	if ($Usuario{'AddDes'.1} ne '1')
	{
	    $EXIST = 0;
	}
	
	#Traer datos de la Descripcion
	%description = &get_data($dbName, "Description", "idDescription", "$idDescription");
    
	if ($description{"idAuthor".1} == $idAuthor)
	{
	    $EXIST = 0;
	}
    }
    #Se guarda la anotacion en la base de datos
    if (($CORRECT == 1) && ($EXIST == 1))
    {
	$idNota = &guarda_anotacion($dbName, $idAuthor, $idDescription, $idTopic, $Nota);
        &insert_historic ($dbName, "\'AddNote\'", $idAuthor, $idTopic, $idDescription, $idNota);
        
	($nota, $nAuthor, $dayN, $idnAuthor) = &traer_nota($dbName, $idNota);
	$titulo = "$nAuthor - $dayN";
	
        #Notificacion de evento
	$Datos{Accion} 	= 12;     #Accion 12 es sAddNote
	$Datos{Autor}		= $idAuthor;
	$Datos{Fecha}		= &get_date();
	$Datos{Titulo}          = $titulo;
	$Datos{Tema}            = $idTopic;
	$Datos{Estructura}      = $idStructure;
	$Datos{Descripcion}	= $idDescription;
	
	&notifyEvent ($dbName, %Datos);
	#Fin de notificacion de eventos

	$msg   = $textos{'inData6'.1};
	$COLOR = "green";
    }
    else
    {
	if ($EXIST == 0)
	{
	    $msg = $textos{'inData7'.1};
	}
	if ($CORRECT == 0)
	{
	    $msg = $textos{'inData8'.1};
	}
	$COLOR = "red";
    }
    
print <<EOF4
    <TABLE BORDER=3 WIDTH=100%>
	<TD ALIGN="CENTER"><FONT COLOR="$COLOR"><b>$msg</b>
    </TABLE>
    <p><CENTER><A HREF=$cancel_accion><IMG SRC="../$IMG/update.gif" BORDER=0></CENTER>
EOF4
    ;
}
$txt = 'anota.htm';
&print_help($dbName, $txt, 'helpError');
print "</BODY>";
print "</HTML>";
